package indi.gao;

import com.alibaba.fastjson.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * <p>Description:</p>
 * <p>Copyright: Copyright (c)2020$</p>
 * <p>Company: Tope</p>
 * <P>Created Date :2020$-03$-10$</P>
 * <P>@version 1.0</P>
 */
public class BirthdayUtil {

    public static int getAgeByBirthday(Date birthDay) {
        int age = 0;
        Calendar cal = Calendar.getInstance();

        // 出生日期晚于当前时间
        if (cal.before(birthDay)) {
            return -1;
        }

        // 当前年月日
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);

        // 生日的年月日
        cal.setTime(birthDay);
        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        // 计算岁数
        age = yearNow - yearBirth;

        // 未过生日age-1
        if (monthNow == monthBirth && dayOfMonthNow < dayOfMonthBirth) {
            age--;
        }
        if (monthNow < monthBirth) {
            age--;
        }

        return age;
    }

    public static Integer getAge(Date birthDay) {
        Calendar cal = Calendar.getInstance();
        if (cal.before(birthDay)) { //出生日期晚于当前时间，无法计算
            return null;
        }
        int yearNow = cal.get(Calendar.YEAR);  //当前年份
        int monthNow = cal.get(Calendar.MONTH);  //当前月份
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); //当前日期
        cal.setTime(birthDay);
        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
        int age = yearNow - yearBirth;   //计算整岁数
        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) {
                    age--;//当前日期在生日之前，年龄减一
                }
            } else {
                age--;//当前月份在生日之前，年龄减一
            }
        }
        return age;
    }

    /**
     * 根据用户生日(yyyy-MM-dd HH:mm:ss)
     * 获取当前用户的年龄
     */
    public static int getCurrentUserAge() throws ParseException {
        int age = 0;
        String birthDay = "2019-04-22";//2016-06-15 +" 00:00:00"

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Date birthday = sdf.parse(birthDay);
        Calendar cal = Calendar.getInstance();
        if (cal.before(birthday)) {
            return 0;
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH) + 1;
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthday);
        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH) + 1;
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
        age = yearNow - yearBirth;
        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow <= dayOfMonthBirth) {//生日当天年龄不增加
                    age--;
                }
            } else {
                age--;
            }
        }

        return age;
    }

    private static int checkReadTicket(Date date) {
        int year = 0;
        long time = System.currentTimeMillis() - date.getTime();
        if (time > 0L) {
            year = (new Double((double) time * 1.0D / 1000.0D / 60.0D / 60.0D / 24.0D / 365.0D)).intValue();
        }
        return year;
    }


    public static void main(String[] args) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = simpleDateFormat.parse("2019-04-22");
        System.out.println("陈亚彤" + getCurrentUserAge());
        System.out.println("interface" + getAgeByBirthday(parse));
        System.out.println("consumer" + getAge(parse));
        System.out.println("张瑞浩" + checkReadTicket(parse));

        ArrayList<BirthdayUtil> birthdayUtils = new ArrayList<>();
        birthdayUtils.add(null);
        birthdayUtils.add(null);
        birthdayUtils.add(new BirthdayUtil());
        birthdayUtils.add(new BirthdayUtil());

        String string = JSONObject.toJSONString(birthdayUtils);
        System.out.println(string);
    }
}